package com.longsichao.lscframe.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.longsichao.lscframe.app.LSCApplication;
import com.longsichao.lscframe.tools.LSCLog;

import okhttp3.MediaType;

/**
 * @author SONG GUANYULONGSICHAO (leo.song[at]outlook[dot]com)
 */
public final class NetworkHelper {
    public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
    public static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    public static final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpeg");

    /**
     * 网络请求方式
     */
    public enum RequestMethod {
        GET, POST,
    }

    /**
     * 蜂窝数据可用状态
     *
     * @return true 可用<br/> false 不可用
     */
    public static boolean isCellularAvailable() {
        ConnectivityManager manager = getConnectivityManager();
        if (manager == null) {
            LSCLog.w("connectivity unavailable");
            return false;
        }
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            LSCLog.w("cellular connection available");
            return true;
        }
        LSCLog.w("cellular connection unavailable");
        return false;
    }

    /**
     * 网络可用状态
     *
     * @return true 可用<br/> false 不可用
     */
    public static boolean isNetworkAvailable() {
        ConnectivityManager connectivity = getConnectivityManager();
        if (connectivity == null) {
            LSCLog.w("connectivity unavailable");
            return false;
        }
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (NetworkInfo anInfo : info) {
                if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                    LSCLog.w(anInfo.getTypeName() + " connection available");
                    return true;
                }
            }
        }
        LSCLog.w("network connection unavailable");
        return false;
    }

    /**
     * 无线网络可用状态
     *
     * @return true 可用<br/> false 不可用
     */
    public static boolean isWirelessAvailable() {
        ConnectivityManager manager = getConnectivityManager();
        if (manager == null) {
            LSCLog.w("connectivity unavailable");
            return false;
        }
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
            LSCLog.w("wireless connection available");
            return true;
        }
        LSCLog.w("wireless connection unavailable");
        return false;
    }

    private static ConnectivityManager getConnectivityManager() {
        return (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    private static Object getSystemService(String name) {
        return LSCApplication.applicationContext.getSystemService(name);
    }

    private NetworkHelper() {
    }
}
